import csv
import json
import os
import numpy as np
import matplotlib.pyplot as plt
from datetime import datetime, timedelta
import matplotlib.dates as mdates


# 设置字体以支持中文
plt.rcParams['font.sans-serif'] = ['SimHei']  # 使用黑体
plt.rcParams['axes.unicode_minus'] = False  # 解决保存图像是负号'-'显示为方块的问题
colors = {
    0: (0, 100, 0),       # 深绿色
    1: (0, 0, 255),       # 蓝色
    2: (139, 0, 0),       # 深红色
    3: (75, 0, 130),      # 深紫色
    4: (139, 69, 19),     # 深棕色
    7: (0, 69, 139),     # 深
    8: (0, 69, 19),     # 深
    10: (139, 139, 19),     # 深
    11: (205, 133, 63),    # 深橙色
    12: (0, 105, 105),     # 深青色
    13: (139, 0, 139),     # 深洋红色
    14: (40, 40, 40),      # 深灰色
    15: (85, 107, 47),     # 深橄榄绿
    16: (0, 105, 148),     # 深海蓝
    21: (255, 0, 139),     # 深酒红
    22: (85, 65, 0),       # 深咖啡色
    23: (22, 55, 22),     # 深苔藓绿
    24: (0, 0, 128),       # 深蓝色
    25: (0, 206, 209),    # 青色
    26: (102, 205, 170)   # Aquamarine
}


def read_json_file(file_path):
    all_data = []
    with open(file_path, 'r', encoding='utf-8') as jsonfile:
        for line in jsonfile:
            data = json.loads(line.strip())
            timestamp = datetime.strptime(data['timestamp'], '%Y-%m-%dT%H:%M:%S.%f')
            all_data.append({
                'timestamp': timestamp,
                'id': os.path.basename(file_path).split('.')[0],
                'flowInfo': data['flowInfo'],
                'durationInfo': data['durationInfo'],
                'passRate': data['passRateInfo'],
                'speed': data['new_row'][5],
                'slowNum': data['new_row'][7],
                'new_row': data['new_row'],
                'new_row_1': data['new_row1'],
            })
    return all_data


def get_jpg_data(all_data):
    flow_data = {}
    pass_data = {}
    speed_data = {}
    slow_data = {}
    duration_data = {}
    all_duration_data = {}
    duration_deal_data = {}
    save_json_data = []
    average_value_data = 0
    for data in all_data:
        timestamp = data['timestamp']
        flow_data[timestamp] = {
            "up_flow": data['flowInfo']['upFlow'],
            "down_flow": data['flowInfo']['downFlow'],
        }
        pass_data[timestamp] = {"pass_rate": data['passRate']}
        speed_data[timestamp] = {"speed": data['speed']}
        slow_data[timestamp] = {"slow_num": data['slowNum']}
        duration_deal_data[timestamp] = {"duration_value": data['new_row_1'][12]}
        for v in data['durationInfo']:
            if v['vtype'] not in duration_data.keys():
                duration_data[v['vtype']] = {}
            duration_time = datetime.strptime(v['timestamp'], '%Y-%m-%dT%H:%M:%S.%f')
            duration_data[v['vtype']][duration_time] = {"carDuration": v['carDuration']}
            all_duration_data[duration_time] = {"carDuration": v['carDuration'], "type": v['vtype']}
        save_json_data.append({
            "timeStamp": timestamp.strftime('%Y-%m-%dT%H:%M:%S.%f'),
            "upFlow": data['new_row'][1],
            "downFlow": data['new_row'][2],
            "vehicleNum": data['new_row'][3],
            "carNum": data['new_row'][4],
            "carSpeed": data['new_row'][5],
            "passRate": data['new_row'][6],
            "slowNum": data['new_row'][7],
            "carDueation": data['new_row'][8],
            "yongduFlag": data['new_row'][9],
            "zuduanFlag": data['new_row'][10],
            "flowChangeFlag": data['new_row_1'][1],
            "flowDiff": data['new_row_1'][2],
            "flowDiffP": data['new_row_1'][3],
            "speedChangeFlag": data['new_row_1'][4],
            "speed60Diff": data['new_row_1'][5],
            "currentSlowNum": data['new_row_1'][6],
            "flowEmptyFlag": data['new_row_1'][7],
            "passFlag": data['new_row_1'][8],
            "currentPassRate": data['new_row_1'][9],
            "intervalDiff": data['new_row_1'][10],
            "durationNum": data['new_row_1'][11],
            "durationValue": data['new_row_1'][12],
            "ave": data['new_row_1'][13],
            "oneFrame": data['new_row_1'][14],
            "blockProbability": data['new_row_1'][15],
        })
    for key, value in duration_data.items():
        value_data = [sub_dict["carDuration"] for sub_key, sub_dict in value.items()]
        filtered_data = np.array(value_data)
        p85 = np.percentile(filtered_data, 85)
        p15 = np.percentile(filtered_data, 0)
        if len(value_data) <= 10:
            filtered_values = value_data
        else:
            filtered_values = [x for x in value_data if p15 < x < p85]
        if filtered_values:
            # 计算过滤后的数据的中位数作为最优值
            optimal_value = np.median(filtered_values)
            average_value = optimal_value
            if key == "1":
                average_value_data = optimal_value
            for key1, value1 in value.items():
                d = value1["carDuration"]
                duration_data[key][key1]["new_seconds"] = min(round(d / average_value * 40), 200)

    return flow_data, pass_data, speed_data, slow_data, duration_data, average_value_data, duration_deal_data, save_json_data


def get_jpg_data_2(all_data):
    flow_data = {}
    pass_data = {}
    speed_data = {}
    slow_data = {}
    duration_data = {}
    all_duration_data = {}
    duration_deal_data = {}
    save_json_data = []
    average_value_data = 0
    for data in all_data:
        timestamp = data['timestamp']
        flow_data[timestamp] = {
            "up_flow": data['flowInfo']['upFlow'],
            "down_flow": data['flowInfo']['downFlow'],
        }
        pass_data[timestamp] = {"pass_rate": data['passRate']}
        speed_data[timestamp] = {"speed": data['speed']}
        slow_data[timestamp] = {"slow_num": data['slowNum']}
        duration_deal_data[timestamp] = {"duration_value": data['new_row_1'][12]}
        for v in data['durationInfo']:
            if v['vtype'] not in duration_data.keys():
                duration_data[v['vtype']] = {}
            duration_time = datetime.strptime(v['timestamp'], '%Y-%m-%dT%H:%M:%S.%f')
            duration_data[v['vtype']][duration_time] = {"carDuration": v['carDuration']}
            all_duration_data[duration_time] = {"carDuration": v['carDuration'], "type": v['vtype']}
        save_json_data.append({
            "timeStamp": timestamp.strftime('%Y-%m-%dT%H:%M:%S.%f'),
            "upFlow": data['new_row_1'][1],
            "downFlow": data['new_row_1'][2],
            "passRate": data['new_row_1'][3],
            "carNum": data['new_row_1'][4],
            "carSpeed": data['new_row_1'][5],
            "vehicleNum": data['new_row_1'][6],
            "allPassRate": data['new_row_1'][7],
            "slowNum": data['new_row_1'][8],
            "carPassTime": data['new_row_1'][9],
            "flowChangeFlag": data['new_row_1'][10],
            "flowDiff": data['new_row_1'][11],
            "flowDiffP": data['new_row_1'][12],
            "speedChangeFlag": data['new_row_1'][13],
            "speed60Diff": data['new_row_1'][14],
            "flowEmptyFlag": data['new_row_1'][15],
            "passFlag": data['new_row_1'][16],
            "intervalDiff": data['new_row_1'][17],
            "durationNum": data['new_row_1'][18],
            "durationValue": data['new_row_1'][19],
            "oneFrame": data['new_row_1'][20],
            "blockFlag": data['new_row_1'][21],
            "blockProbability": data['new_row_1'][22],
            "yongduFlag": data['new_row_1'][23],
            "zuduanFlag": data['new_row_1'][24],
        })
    for key, value in duration_data.items():
        value_data = [sub_dict["carDuration"] for sub_key, sub_dict in value.items()]
        filtered_data = np.array(value_data)
        p85 = np.percentile(filtered_data, 85)
        p15 = np.percentile(filtered_data, 0)
        if len(value_data) <= 10:
            filtered_values = value_data
        else:
            filtered_values = [x for x in value_data if p15 < x < p85]
        if filtered_values:
            # 计算过滤后的数据的中位数作为最优值
            optimal_value = np.median(filtered_values)
            average_value = optimal_value
            if key == "1":
                average_value_data = optimal_value
            for key1, value1 in value.items():
                d = value1["carDuration"]
                duration_data[key][key1]["new_seconds"] = min(round(d / average_value * 40), 200)

    return flow_data, pass_data, speed_data, slow_data, duration_data, average_value_data, duration_deal_data, save_json_data



def show_pass_rate(pass_data, path, json_name):
    # 提取X轴时间数据
    times = list(pass_data.keys())
    # 提取Y轴数据
    percentage = [d['pass_rate'] for d in pass_data.values() if 'pass_rate' in d]
    # 绘图
    plt.plot(times, percentage, marker='o', markersize=3, linestyle='-', color='r', label='通过率')

    # 设置X轴为每1小时一个标记
    ax = plt.gca()  # 获取当前的Axes
    fig = ax.figure
    fig.set_size_inches(20, 12)
    ax.xaxis.set_major_locator(mdates.MinuteLocator(byminute=[0, 30], interval=1))
    ax.xaxis.set_major_formatter(mdates.DateFormatter('%H:%M'))  # 设置时间格式
    # 设置X轴时间范围固定为00:00到23:59
    start_time = times[10].replace(hour=0, minute=0, second=0, microsecond=0)
    end_time = times[10].replace(hour=23, minute=59, second=59, microsecond=999999)
    ax.set_xlim(start_time, end_time)
    plt.gcf().autofmt_xdate()  # 自动调整X轴日期标签的格式
    plt.ylim(-10, 110)
    # 设置Y轴刻度，每隔20显示一个
    yticks = np.arange(-10, 111, 10)  # 生成从0到200（包括200），步长为20的数组
    plt.yticks(yticks)
    # 可选：添加网格线以提高可读性
    plt.grid(True, which='both', linestyle='--', linewidth=0.5)

    # 添加标签和标题
    ax.set_ylabel('通过率（%）')

    ax.set_title('小客车通过率')
    ax.legend(loc='lower right')

    # # 显示图表
    # plt.show()

    # 保存图表到文件
    output_dir = os.path.join(path, json_name)
    if not os.path.exists(output_dir):
        os.makedirs(output_dir)
    file_name = "real_car_pass_rate_data"
    output_filename = os.path.join(output_dir, file_name + '.png')
    plt.savefig(output_filename, dpi=200, bbox_inches='tight', transparent=False)
    # 关闭图表以释放内存
    plt.close()


def show_slow(slow_data, path, json_name):
    # 提取数据
    times = list(slow_data.keys())
    data_list = [d['slow_num'] for d in slow_data.values() if 'slow_num' in d]
    count = [100 if int(num) > 100 else int(num) for num in data_list]
    plt.plot(times, count, marker='o', markersize=3, linestyle='-', color='r', label='慢行车数量')

    ax = plt.gca()
    fig = ax.figure
    fig.set_size_inches(20, 12)
    ax.xaxis.set_major_locator(mdates.MinuteLocator(byminute=[0, 30], interval=1))
    ax.xaxis.set_major_formatter(mdates.DateFormatter('%H:%M'))  # 设置时间格式
    # 设置X轴时间范围固定为00:00到23:59
    start_time = times[10].replace(hour=0, minute=0, second=0, microsecond=0)
    end_time = times[10].replace(hour=23, minute=59, second=59, microsecond=999999)
    ax.set_xlim(start_time, end_time)
    plt.gcf().autofmt_xdate()  # 自动调整X轴日期标签的格式
    # 可选：添加网格线以提高可读性
    plt.grid(True, which='both', linestyle='--', linewidth=0.5)

    # 设置Y轴每隔10进行显示
    y_min, y_max = ax.get_ylim()  # 获取当前Y轴的最小值和最大值
    # 找到最接近y_min但不大于y_min的10的倍数
    start = np.floor(y_min / 10) * 10
    # 找到最接近y_max但不小于y_max的10的倍数
    end = np.ceil(y_max / 10) * 10
    plt.yticks(np.arange(start, end + 1, 10))  # 设定Y轴的刻度间隔为10，并确保是整十

    # 添加标签和标题
    ax.set_ylabel('数量（辆）')
    ax.set_title('小客车全天慢行数量统计')

    # # 显示图表
    # plt.show()

    # 保存图表到文件
    output_dir = os.path.join(path, json_name)
    if not os.path.exists(output_dir):
        os.makedirs(output_dir)
    file_name = "real_car_slow_move_data"
    output_filename = os.path.join(output_dir, file_name + '.png')
    plt.savefig(output_filename, dpi=200, bbox_inches='tight', transparent=False)
    # 关闭图表以释放内存
    plt.close()


def show_speed(speed_data, path, json_name):
    # 提取数据
    times = list(speed_data.keys())
    mean_speeds = [d['speed'] for d in speed_data.values() if 'speed' in d]
    plt.plot(times, mean_speeds, marker='o', markersize=3, linestyle='-', color='r', label='平均速度')
    plt.axhline(y=40, color='b', linestyle='--', label='40km/h')

    # 设置X轴为每1小时一个标记
    ax = plt.gca()  # 获取当前的Axes
    fig = ax.figure
    fig.set_size_inches(20, 12)
    ax.xaxis.set_major_locator(mdates.MinuteLocator(byminute=[0, 30], interval=1))
    ax.xaxis.set_major_formatter(mdates.DateFormatter('%H:%M'))  # 设置时间格式
    # 设置X轴时间范围固定为00:00到23:59
    start_time = times[10].replace(hour=0, minute=0, second=0, microsecond=0)
    end_time = times[10].replace(hour=23, minute=59, second=59, microsecond=999999)
    ax.set_xlim(start_time, end_time)
    plt.gcf().autofmt_xdate()  # 自动调整X轴日期标签的格式
    # 可选：添加网格线以提高可读性
    plt.grid(True, which='both', linestyle='--', linewidth=0.5)

    # 设置Y轴每隔10进行显示
    y_min, y_max = ax.get_ylim()  # 获取当前Y轴的最小值和最大值
    # 找到最接近y_min但不大于y_min的10的倍数
    start = np.floor(y_min / 10) * 10
    # 找到最接近y_max但不小于y_max的10的倍数
    end = np.ceil(y_max / 10) * 10
    plt.yticks(np.arange(start, end + 1, 10))  # 设定Y轴的刻度间隔为10，并确保是整十

    # 添加标签和标题
    ax.set_ylabel('速度值（千米/小时）')
    ax.set_title('小客车全天平均速度统计')

    # # 显示图表
    # plt.show()

    # 保存图表到文件
    output_dir = os.path.join(path, json_name)
    if not os.path.exists(output_dir):
        os.makedirs(output_dir)
    file_name = "real_car_time_data"
    output_filename = os.path.join(output_dir, file_name + '.png')
    plt.savefig(output_filename, dpi=200, bbox_inches='tight', transparent=False)
    # 关闭图表以释放内存
    plt.close()


def show_duration(duration_data, average_value_data, path, json_name, flag):

    if flag:
        k = "1"
        v = duration_data[k]
        duration_seconds = []
        times = list(v.keys())
        for key, value in v.items():
            duration_seconds.append(value['new_seconds'])
        data = {
            'x': times,
            'y': duration_seconds
        }
        # 绘制散点图
        plt.scatter(data['x'], data['y'], color=[c / 255 for c in colors[int(k)]], label='车型：' + str(k), s=10)
        plt.axhline(y=20, color='g', linestyle='--', label='20线')
        plt.axhline(y=60, color='y', linestyle='--', label='60线')
        xlim = plt.xlim()
        # 在水平线旁添加文本，选择靠近左边界的位置放置文本
        avg_time_str = str(int(average_value_data)) + "秒"
        plt.text(xlim[0], 40, avg_time_str, verticalalignment='center', color='black')
    else:
        for k, v in duration_data.items():
            duration_seconds = []
            times = list(v.keys())
            for key, value in v.items():
                duration_seconds.append(value['new_seconds'])
            data = {
                'x': times,
                'y': duration_seconds
            }
            # 绘制散点图
            plt.scatter(data['x'], data['y'], color=[c / 255 for c in colors[int(k)]], label='车型：' + str(k), s=10)

        plt.axhline(y=20, color='g', linestyle='--', label='20线')
        plt.axhline(y=60, color='y', linestyle='--', label='60线')
        xlim = plt.xlim()
        # 在水平线旁添加文本，选择靠近左边界的位置放置文本
        avg_time_str = str(int(average_value_data)) + "秒"
        plt.text(xlim[0], 40, avg_time_str, verticalalignment='center', color='black')

    ax = plt.gca()
    fig = ax.figure
    fig.set_size_inches(20, 12)
    ax.xaxis.set_major_locator(mdates.MinuteLocator(byminute=[0, 30], interval=1))
    ax.xaxis.set_major_formatter(mdates.DateFormatter('%H:%M'))
    # 设置X轴时间范围固定为00:00到23:59
    day = list(duration_data["1"].keys())[10]
    start_time = day.replace(hour=0, minute=0, second=0, microsecond=0)
    end_time = day.replace(hour=23, minute=59, second=59, microsecond=999999)
    ax.set_xlim(start_time, end_time)
    plt.gcf().autofmt_xdate()  # 自动调整X轴日期标签的格式
    plt.ylim(0, 200)
    # 设置Y轴刻度，每隔20显示一个
    yticks = np.arange(0, 201, 20)  # 生成从0到200（包括200），步长为20的数组
    plt.yticks(yticks)
    # 可选：添加网格线以提高可读性
    plt.grid(True, which='both', linestyle='--', linewidth=0.5)

    # 添加标签和标题
    ax.set_ylabel('通行时间（秒）')
    if flag:
        ax.set_title('小客车通行散点图')
    else:
        ax.set_title('所有车型通行散点图')
    ax.legend(loc='upper right')

    # # 显示图表
    # plt.show()

    # 保存图表到文件
    output_dir = os.path.join(path, json_name)
    if not os.path.exists(output_dir):
        os.makedirs(output_dir)
    if flag:
        file_name = "real_time_duration_data_car"
    else:
        file_name = "real_time_duration_data_all"
    output_filename = os.path.join(output_dir, file_name + '.png')
    plt.savefig(output_filename, dpi=200, bbox_inches='tight', transparent=False)
    # 关闭图表以释放内存
    plt.close()


def show_time_duration(duration_data, average_value_data, path, json_name, flag, file_name=None):

    if flag:
        k = "1"
        v = duration_data[k]
        duration_seconds = []
        times = list(v.keys())
        for key, value in v.items():
            duration_seconds.append(value['carDuration'])
        data = {
            'x': times,
            'y': duration_seconds
        }
        # 绘制散点图
        plt.scatter(data['x'], data['y'], color=[c / 255 for c in colors[int(k)]], label='车型：' + str(k), s=10)
    else:
        for k, v in duration_data.items():
            duration_seconds = []
            times = list(v.keys())
            for key, value in v.items():
                duration_seconds.append(value['carDuration'])
            data = {
                'x': times,
                'y': duration_seconds
            }
            # 绘制散点图
            plt.scatter(data['x'], data['y'], color=[c / 255 for c in colors[int(k)]], label='车型：' + str(k), s=10)

    ax = plt.gca()
    fig = ax.figure
    fig.set_size_inches(20, 12)
    ax.xaxis.set_major_locator(mdates.MinuteLocator(byminute=[0, 30], interval=1))
    ax.xaxis.set_major_formatter(mdates.DateFormatter('%H:%M'))
    # 设置X轴时间范围固定为00:00到23:59
    day = list(duration_data["1"].keys())[10]
    start_time = day.replace(hour=0, minute=0, second=0, microsecond=0)
    end_time = day.replace(hour=23, minute=59, second=59, microsecond=999999)
    ax.set_xlim(start_time, end_time)
    plt.gcf().autofmt_xdate()  # 自动调整X轴日期标签的格式
    # 可选：添加网格线以提高可读性
    plt.grid(True, which='both', linestyle='--', linewidth=0.5)

    # 添加标签和标题
    ax.set_ylabel('通行时间（秒）')
    if flag:
        ax.set_title('小客车通行散点图')
    else:
        ax.set_title('所有车型通行散点图')
    ax.legend(loc='upper right')

    # # 显示图表
    # plt.show()

    # 保存图表到文件
    output_dir = os.path.join(path, json_name)
    if not os.path.exists(output_dir):
        os.makedirs(output_dir)
    if flag and file_name is None:
        file_name = "real_alll_time_duration_data_car"
    elif file_name is None:
        file_name = "real_alll_time_duration_data_all"
    output_filename = os.path.join(output_dir, file_name + '.png')
    plt.savefig(output_filename, dpi=200, bbox_inches='tight', transparent=False)
    # 关闭图表以释放内存
    plt.close()


def show_flow(flow_data, path, json_name):
    # 提取数据
    times = list(flow_data.keys())
    up_flow = [d['up_flow'] for d in flow_data.values() if 'up_flow' in d]
    down_flow = [d['down_flow'] for d in flow_data.values() if 'down_flow' in d]

    plt.plot(times, up_flow,  marker=',', linestyle='-', color='r', label='上游流量')
    plt.plot(times, down_flow,  marker=',', linestyle='-', color='b', label='下游流量')

    ax = plt.gca()
    fig = ax.figure
    fig.set_size_inches(20, 12)
    ax.xaxis.set_major_locator(mdates.MinuteLocator(byminute=[0, 30], interval=1))
    ax.xaxis.set_major_formatter(mdates.DateFormatter('%H:%M'))  # 设置时间格式
    # 设置X轴时间范围固定为00:00到23:59
    start_time = times[10].replace(hour=0, minute=0, second=0, microsecond=0)
    end_time = times[10].replace(hour=23, minute=59, second=59, microsecond=999999)
    ax.set_xlim(start_time, end_time)
    plt.gcf().autofmt_xdate()  # 自动调整X轴日期标签的格式
    # 可选：添加网格线以提高可读性
    plt.grid(True, which='both', linestyle='--', linewidth=0.5)

    # 添加标签和标题
    ax.set_ylabel('流量（辆）')
    ax.set_title('上下游流量分布数据统计')
    ax.legend(loc='upper left')

    # # 显示图表
    # plt.show()

    # 保存图表到文件
    output_dir = os.path.join(path, json_name)
    if not os.path.exists(output_dir):
        os.makedirs(output_dir)
    file_name = "real_alll_flow_data"
    output_filename = os.path.join(output_dir, file_name + '.png')
    plt.savefig(output_filename, dpi=200, bbox_inches='tight', transparent=False)
    # 关闭图表以释放内存
    plt.close()


def show_flow_diff(flow_data, path, json_name):
    # 提取数据
    times = list(flow_data.keys())
    flow_diff = [(d['up_flow']-d['down_flow']) for d in flow_data.values() if 'up_flow' in d]

    # plt.plot(times, flow_diff,  marker=',', linestyle='-', color='r', label='上下游流量差值')
    # 根据差值正负设置颜色
    colors = ['red' if diff > 0 else 'blue' for diff in flow_diff]
    # 动态调整柱子宽度
    num_data = len(times)
    if num_data > 0:
        width = 0.8 / num_data
    else:
        width = 0.8
    plt.bar(times, flow_diff, color=colors, width=width, label='上下游流量差值')

    ax = plt.gca()
    fig = ax.figure
    fig.set_size_inches(20, 12)
    ax.xaxis.set_major_locator(mdates.MinuteLocator(byminute=[0, 30], interval=1))
    ax.xaxis.set_major_formatter(mdates.DateFormatter('%H:%M'))  # 设置时间格式
    # 设置X轴时间范围固定为00:00到23:59
    start_time = times[10].replace(hour=0, minute=0, second=0, microsecond=0)
    end_time = times[10].replace(hour=23, minute=59, second=59, microsecond=999999)
    ax.set_xlim(start_time, end_time)
    plt.gcf().autofmt_xdate()  # 自动调整X轴日期标签的格式
    # 可选：添加网格线以提高可读性
    plt.grid(True, which='both', linestyle='--', linewidth=0.5)

    # 添加标签和标题
    ax.set_ylabel('流量（辆）')
    ax.set_title('上下游流量差值统计')
    ax.legend(loc='upper left')

    # # 显示图表
    # plt.show()

    # 保存图表到文件
    output_dir = os.path.join(path, json_name)
    if not os.path.exists(output_dir):
        os.makedirs(output_dir)
    file_name = "real_flow_diff_data"
    output_filename = os.path.join(output_dir, file_name + '.png')
    plt.savefig(output_filename, dpi=200, bbox_inches='tight', transparent=False)
    # 关闭图表以释放内存
    plt.close()


def show_flow_diff_value(flow_data, path, json_name):
    # 提取数据
    times = list(flow_data.keys())
    flow_diff = [(d['up_flow']-d['down_flow']) for d in flow_data.values() if 'up_flow' in d]
    up = []
    down = []
    data_diff = []
    for i in range(len(flow_diff)):
        if flow_diff[i] < 0:
            down.append(-flow_diff[i])
        else:
            up.append(flow_diff[i])
        data_diff.append(sum(up) - sum(down))

    plt.plot(times, data_diff,  marker='.', linestyle='-', color='r', label='滞留车辆')

    ax = plt.gca()
    fig = ax.figure
    fig.set_size_inches(20, 12)
    ax.xaxis.set_major_locator(mdates.MinuteLocator(byminute=[0, 30], interval=1))
    ax.xaxis.set_major_formatter(mdates.DateFormatter('%H:%M'))  # 设置时间格式
    # 设置X轴时间范围固定为00:00到23:59
    start_time = times[10].replace(hour=0, minute=0, second=0, microsecond=0)
    end_time = times[10].replace(hour=23, minute=59, second=59, microsecond=999999)
    ax.set_xlim(start_time, end_time)
    plt.gcf().autofmt_xdate()  # 自动调整X轴日期标签的格式
    # 可选：添加网格线以提高可读性
    plt.grid(True, which='both', linestyle='--', linewidth=0.5)

    # 添加标签和标题
    ax.set_ylabel('流量（辆）')
    ax.set_title('滞留车辆统计')
    ax.legend(loc='upper left')

    # # 显示图表
    # plt.show()

    # 保存图表到文件
    output_dir = os.path.join(path, json_name)
    if not os.path.exists(output_dir):
        os.makedirs(output_dir)
    file_name = "real_flow_diff_value_data"
    output_filename = os.path.join(output_dir, file_name + '.png')
    plt.savefig(output_filename, dpi=200, bbox_inches='tight', transparent=False)
    # 关闭图表以释放内存
    plt.close()


def show_time_deal(duration_deal_data, path, json_name):
    # 提取数据
    times = list(duration_deal_data.keys())
    duration_value = [d['duration_value'] for d in duration_deal_data.values() if 'duration_value' in d]
    for i in range(len(duration_value)):
        if duration_value[i] > 200:
            duration_value[i] = 200
        if i > 2 and duration_value[i] == 0 and duration_value[i - 1] > 0:
            duration_value[i] = duration_value[i - 1]
        elif duration_value[i] == 0:
            duration_value[i] = 40

    plt.plot(times, duration_value,  marker=',', linestyle='-', color='r', label='小客车通行时间均线')
    plt.axhline(y=20, color='g', linestyle='--', label='20线')
    plt.axhline(y=60, color='y', linestyle='--', label='60线')
    plt.axhline(y=40, color='black', linestyle='--', label='40线')

    ax = plt.gca()
    fig = ax.figure
    fig.set_size_inches(20, 12)
    ax.xaxis.set_major_locator(mdates.MinuteLocator(byminute=[0, 30], interval=1))
    ax.xaxis.set_major_formatter(mdates.DateFormatter('%H:%M'))  # 设置时间格式
    # 设置X轴时间范围固定为00:00到23:59
    start_time = times[10].replace(hour=0, minute=0, second=0, microsecond=0)
    end_time = times[10].replace(hour=23, minute=59, second=59, microsecond=999999)
    ax.set_xlim(start_time, end_time)
    plt.gcf().autofmt_xdate()  # 自动调整X轴日期标签的格式
    plt.ylim(0, 220)
    # 设置Y轴刻度，每隔20显示一个
    yticks = np.arange(0, 221, 20)  # 生成从0到200（包括200），步长为20的数组
    plt.yticks(yticks)
    # 可选：添加网格线以提高可读性
    plt.grid(True, which='both', linestyle='--', linewidth=0.5)

    # 添加标签和标题
    ax.set_ylabel('时间值')
    ax.set_title('小客车通行时间统计')
    ax.legend(loc='upper left')

    # # 显示图表
    # plt.show()

    # 保存图表到文件
    output_dir = os.path.join(path, json_name)
    if not os.path.exists(output_dir):
        os.makedirs(output_dir)
    file_name = "real_duration_value_data"
    output_filename = os.path.join(output_dir, file_name + '.png')
    plt.savefig(output_filename, dpi=200, bbox_inches='tight', transparent=False)
    # 关闭图表以释放内存
    plt.close()


def show_time_pass(duration_deal_data, path, json_name, value=55):
    # 提取数据
    times = list(duration_deal_data.keys())
    duration_value = [d['duration_value'] for d in duration_deal_data.values() if 'duration_value' in d]
    for i in range(len(duration_value)):
        if duration_value[i] > 200:
            duration_value[i] = 200
        if i > 2 and duration_value[i] == 0 and duration_value[i-1] > 0:
            duration_value[i] = duration_value[i-1]
        elif duration_value[i] == 0:
            duration_value[i] = 40

    data = []
    for i in range(len(duration_value)):
        if duration_value[i] > value:
            data.append(0)
        else:
            data.append(100)
    plt.plot(times, data,  marker=',', linestyle='-', color='r', label='小客车通过率')

    ax = plt.gca()
    fig = ax.figure
    fig.set_size_inches(20, 12)
    ax.xaxis.set_major_locator(mdates.MinuteLocator(byminute=[0, 30], interval=1))
    ax.xaxis.set_major_formatter(mdates.DateFormatter('%H:%M'))  # 设置时间格式
    # 设置X轴时间范围固定为00:00到23:59
    start_time = times[10].replace(hour=0, minute=0, second=0, microsecond=0)
    end_time = times[10].replace(hour=23, minute=59, second=59, microsecond=999999)
    ax.set_xlim(start_time, end_time)
    plt.gcf().autofmt_xdate()  # 自动调整X轴日期标签的格式
    # 设置Y轴每隔10进行显示
    y_min, y_max = ax.get_ylim()  # 获取当前Y轴的最小值和最大值
    # 找到最接近y_min但不大于y_min的10的倍数
    start = np.floor(y_min / 10) * 10
    # 找到最接近y_max但不小于y_max的10的倍数
    end = np.ceil(y_max / 10) * 10
    plt.yticks(np.arange(start, end + 1, 10))  # 设定Y轴的刻度间隔为10，并确保是整十
    # 可选：添加网格线以提高可读性
    plt.grid(True, which='both', linestyle='--', linewidth=0.5)

    # 添加标签和标题
    ax.set_ylabel('时间值')
    ax.set_title('小客车通过率')
    ax.legend(loc='upper left')

    # # 显示图表
    # plt.show()

    # 保存图表到文件
    output_dir = os.path.join(path, json_name)
    if not os.path.exists(output_dir):
        os.makedirs(output_dir)
    file_name = "real_car_duration_value_data_" + str(value)
    output_filename = os.path.join(output_dir, file_name + '.png')
    plt.savefig(output_filename, dpi=200, bbox_inches='tight', transparent=False)
    # 关闭图表以释放内存
    plt.close()


def save_json(save_json_data, path, json_name):
    output_dir = os.path.join(path, json_name)
    if not os.path.exists(output_dir):
        os.makedirs(output_dir)
    file_name = "all_data"
    output_filename = os.path.join(output_dir, file_name + '.json')
    with open(output_filename, 'w', encoding='utf-8') as json_file:
        for data in save_json_data:
            json_file.write(json.dumps(data, ensure_ascii=False))
            json_file.write('\n')


def traverse_json_files(directory):
    json_files = []
    for root, dirs, files in os.walk(directory):
        for file in files:
            if file.endswith('.json'):
                json_files.append(os.path.join(root, file))
    return json_files


def save_csv_file(file_path, all_data, name):
    try:
        with open(file_path, 'w', newline='', encoding='utf-8') as csv_file:
            writer = csv.writer(csv_file)
            if name == 'new_row':
                writer.writerow(['时间', '上游流量', '下游流量', '区间车辆数', '小客车数量', '小客车平均车速', '小客车通过率', '小客车通过数量', '小客车慢行数量', '小客车平均通行时间(s)', '路况', '事故'])
            if name == 'new_row_1':
                writer.writerow(['time', 'flow_change_flag', 'flow_diff', 'flow_diff_p', 'speed_change_flag', 'speed_60_diff', 'current_slow_num', 'flow_empty_flag', 'pass_flag', 'current_pass_rate', 'interval_diff', 'duration_num', 'duration_value', 'ave', 'one_frame', 'block_probability', 'content'])
            for data in all_data:
                csv_data = data[name]
                writer.writerow(csv_data)
    except Exception as e:
        print("csv写入失败 Error:", e)


def deal_duration_data(all_duration_data):
    # 定义时间粒度和滑动步长
    window_size = timedelta(minutes=5)
    step = timedelta(minutes=1)

    # 存储处理后的数据
    new_duration_data = {}
    for k, v in all_duration_data.items():
        # 将字典转换为元组列表，并按时间排序
        sorted_data = sorted(v.items(), key=lambda item: item[0])
        # 找到数据中的最小和最大时间
        min_time = sorted_data[0][0]
        max_time = sorted_data[-1][0]

        # 初始化时间窗口
        current_time = min_time
        end_time = min_time + window_size

        while current_time <= max_time:
            window_data = {}
            car_duration_values = []
            for time, value in v.items():
                if current_time <= time < end_time:
                    window_data[time] = value
                    car_duration_values.append(value['carDuration'])
            # 计算85百分位的值
            percentile_85 = np.percentile(car_duration_values, 85) if car_duration_values else 0
            # 剔除高于85百分位的异常数据
            filtered_window_data = {k: v for k, v in window_data.items() if v['carDuration'] <= percentile_85}

            for k1, v1 in filtered_window_data.items():
                if k not in new_duration_data.keys():
                    new_duration_data[k] = {}
                new_duration_data[k][k1] = v1

            # 滑动时间窗口
            current_time += step
            end_time += step

    return new_duration_data


def deal_output_json_log(folder_path, PATH):
    json_files_list = traverse_json_files(folder_path)
    for file_path in json_files_list:

        # file_path = r'D:\下载\飞书文件下载\20240325-G000551003000320010-G000551003000220010.csv.json'

        all_data = read_json_file(file_path)

        flow_data, pass_data, speed_data, slow_data, duration_data, average_value_data, duration_deal_data, save_json_data = get_jpg_data(all_data)

        new_duration_data = deal_duration_data(duration_data)

        # print("flow_data:", flow_data)
        # print("pass_data:", pass_data)
        # print("speed_data:", speed_data)
        # print("slow_data:", slow_data)
        # print("duration_data:", duration_data)
        # path = os.path.join(PATH, 'save_png')
        # if not os.path.exists(path):
        #     os.makedirs(path)
        json_name = os.path.basename(file_path).split('.')[0]

        path1 = os.path.join(PATH, 'save_csv')
        if not os.path.exists(path1):
            os.makedirs(path1)
        csv_file_path1 = os.path.join(path1, str(json_name) + '.csv')
        save_csv_file(csv_file_path1, all_data, 'new_row')

        # path2 = os.path.join(PATH, 'save_csv_1')
        # if not os.path.exists(path2):
        #     os.makedirs(path2)
        # csv_file_path2 = os.path.join(path2, str(json_name) + '.csv')
        # save_csv_file(csv_file_path2, all_data, 'new_row_1')

        # show_pass_rate(pass_data, path, json_name)
        # show_slow(slow_data, path, json_name)
        # show_flow(flow_data, path, json_name)
        # show_speed(speed_data, path, json_name)
        # show_flow_diff(flow_data, path, json_name)

        # show_time_duration(duration_data, average_value_data, path, json_name, flag=False, file_name="real_alll_time_duration_data_all_1")
        # show_time_duration(new_duration_data, average_value_data, path, json_name, flag=False, file_name="real_alll_time_duration_data_all_2")
        # show_time_duration(new_duration_data, average_value_data, path, json_name, flag=True, file_name="real_alll_time_duration_data_all_3")
        # show_duration(new_duration_data, save_json_data[-1]["ave"], path, json_name, flag=True)
        # show_time_deal(duration_deal_data, path, json_name)
        # # save_json(save_json_data, path, json_name)
        # show_time_pass(duration_deal_data, path, json_name, value=50)
        # show_time_pass(duration_deal_data, path, json_name, value=100)
        # # show_flow_diff_value(flow_data, path, json_name)


if __name__ == '__main__':
    folder = r'D:\GJ\项目\事故检测\output0311\0320\2025-03-20_15-25-42'
    ppath = r'D:\GJ\项目\事故检测\output0311\0320'
    print(ppath)
    deal_output_json_log(folder, ppath)

    # folder1 = r'D:\GJ\项目\事故检测\output0311\11\all'
    # ppath1 = r'D:\GJ\项目\事故检测\output0311\11'
    # print(ppath1)
    # main(folder1, ppath1)
    #
    # folder2 = r'D:\GJ\项目\事故检测\output0311\12\all'
    # ppath2 = r'D:\GJ\项目\事故检测\output0311\12'
    # print(ppath2)
    # main(folder2, ppath2)

    # file_path = r'D:\下载\飞书文件下载\20240325-G000551003000320010-G000551003000220010.csv.json'
    # all_data = read_json_file(file_path)
    # csv_file_path1 = r'D:\GJ\项目\铜锣山\0306_1\1.csv'
    # save_csv_file(csv_file_path1, all_data, 'new_row')

